﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DiscoveryLogic.UI.Layouts.Converters;
using DiscoveryLogic.UI.Layouts.Models;

namespace DiscoveryLogic.UI.Layouts.Controls
{
    public partial class BarChart : UserControl
    {

        #region Private Variables

            private double axisTop;
            private double axisLength;


            private double minValue, maxValue;
            private double barValue;
            private double goalValue;

            private double currentAnnualIncome;
            private double currentAge;

            private double contributionPercent;
            private double retirementAge;
            private int investmentStyle;

            private RetirementPlan retirementPlan;

        #endregion

        #region Constructor

            public BarChart(RetirementPlan plan)
            {
                // Required to initialize variables
                InitializeComponent();

                retirementPlan = plan;
                retirementPlan.planChange += new EventHandler(OnPlanChange);

                minValue = 0;
                maxValue = 1000000;

                axisLength = BarChartAxis.Height;
                axisTop = (double)BarChartAxis.GetValue(Canvas.TopProperty);

                // Set current income and age
                UserProfile user = new UserProfile();
                currentAnnualIncome = user.AnnualIncome;
                TimeSpan ts = DateTime.Today - user.DateOfBirth;
                currentAge = Convert.ToInt32(ts.TotalDays / 360.00);
            }
        #endregion

        #region Public Properties

            public double CurrentAnnualIncome
            {
                get { return currentAnnualIncome; }
                set 
                { 
                    currentAnnualIncome = value;
                    Redraw();
                }
            }

            public double CurrentAge
            {
                get { return currentAge; }
                set 
                {
                    currentAge = value;
                    Redraw();
                }
            }

            public double ContributionPercent
            {
                get { return contributionPercent; }
                set 
                { 
                    contributionPercent = value;
                    Redraw();
                }
            }

            public double RetirementAge
            {
                get { return retirementAge; }
                set 
                { 
                    retirementAge = value;
                    Redraw();
                }
            }

            public int InvestmentStyle
            {
                get { return investmentStyle; }
                set 
                { 
                    investmentStyle = value;
                    Redraw();
                }
            }

        #endregion

        #region Public Methods
        #endregion

        #region Helper Methods

            private void Redraw()
            {   
                // Calculate how much the contribution amount grows to by retirement                
                double percent;
                
                // Find value of bar
                barValue = retirementPlan.TotalIncomeAtRetirement;
                
                // Find value of goal
                if (retirementPlan.PlanRange > 0)
                {
                    // Set goal indicator position and label          
                    double[] essentialExpenses = retirementPlan.EssentialExpensesArray;
                    double[] income = retirementPlan.IncomeArray;
                    goalValue = essentialExpenses[retirementPlan.PlanRange - 1] -
                                  (income[retirementPlan.PlanRange - 1] - retirementPlan.TotalIncomeAtRetirement);
                }

                if (goalValue <= 0)
                {
                    GoalIndicatorCanvas.Opacity = 0;
                }
                else
                {
                    GoalIndicatorCanvas.Opacity = 1;
                }
                
                
                // Set new max value
                if (barValue > maxValue || goalValue > maxValue)
                {
                    maxValue = maxValue * 2;

                    if (goalValue > maxValue)
                    {
                        maxValue = goalValue + 1000000;
                    }
                    if (barValue > maxValue)
                    {
                        maxValue = barValue + 1000000;
                    }
                }
                else if (barValue < maxValue / 2 && goalValue < maxValue / 2)
                {
                    maxValue = maxValue / 2;
                }

                double length = maxValue - minValue;

                // Set bar position and height                                              
                percent = (barValue - minValue) / length;
                double barTop = axisTop + axisLength - (axisLength * percent);
                double barHeight = axisTop + axisLength - barTop;
                
                try
                {                    
                    BarChartHeightAnimKeyFrame.Value = barHeight;
                    BarChartTopAnimKeyFrame.Value = barTop;
                    BarChartAnim.Begin();
                }
                catch {}

                // Set bar2 position and height                                              
                percent = (barValue * 0.5 - minValue) / length;
                double bar2Top = axisTop + axisLength - (axisLength * percent);
                double bar2Height = axisTop + axisLength - bar2Top;

                try
                {
                    BarChart2HeightAnimKeyFrame.Value = bar2Height;
                    BarChart2TopAnimKeyFrame.Value = bar2Top;
                    BarChart2Anim.Begin();
                }
                catch { }

                // Set goal position
                percent = (goalValue - minValue) / length;
                double goalTop = axisTop + axisLength - (axisLength * percent);

                try
                {
                    BarChartGoalTopAnimKeyFrame.Value = goalTop;
                    BarChartGoalAnim.Begin();
                }
                catch  {}                    

                // Set labels
                BarValueLabel.Text = Convert.ToInt32(barValue).ToCurrency();
                Bar2ValueLabel.Text = Convert.ToInt32(barValue * 0.5).ToCurrency();
                GoalValueLabel.Text = Convert.ToInt32(goalValue).ToCurrency();
                BarChartAmountMaxLabel.Text = Convert.ToInt32(maxValue).ToCurrency();
                //BarChartAmountMaxLabel.SetValue(Canvas.LeftProperty, -56.141 - (2 * maxValue.ToString().Length));
            }

        #endregion

        #region Event Handlers

            private void OnPlanChange(object sender, EventArgs e)
            {
                Redraw();
            }

        #endregion
    }
}
